package edu.hawaii.myisern.action;

import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

/**
 * Tests operation of the MyIsern web pages.
 * 
 * @author Randy Cox
 */
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public class TestIndexActionBean {

  /** Get the test host. */
  private String Url = System.getProperty("test.host") + "myisern-tan";
  private String success = "You have logged in successfully!";

  /**
   * Test the login button.
   * 
   * @throws Exception If problem occurs.
   */
  @Test
  public void testIndexLoginButton() throws Exception {

    WebConversation conversation = new WebConversation();

    // Get login page and check title
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Checking index.jsp retrieval", "MyIsern 2.0", response.getTitle());
    String text = response.getText();
    assertTrue("Check for login.", text.contains("Username:"));

    // Push login button with empty fields
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest request = loginForm.getRequest();
    response = conversation.getResponse(request);
    text = response.getText();
    assertTrue("Check if error message is correct", text.contains("Username is a required field"));
    assertTrue("Check if error message is correct", text.contains("Password is a required field"));

    // Push login button with bad user
    loginForm = response.getFormWithID("LoginForm");
    request = loginForm.getRequest();
    request.setParameter("username", "baduser");
    request.setParameter("password", "badpass");
    response = conversation.getResponse(request);
    text = response.getText();
    assertTrue("Check if error message is correct", text.contains("Invalid User and/or Password"));

    // Push login button with bad password
    loginForm = response.getFormWithID("LoginForm");
    request = loginForm.getRequest();
    request.setParameter("username", "user");
    request.setParameter("password", "badpass");
    response = conversation.getResponse(request);
    text = response.getText();
    assertTrue("Check if error message is correct", text.contains("Invalid User and/or Password"));

    // Push login button with correct user and password
    loginForm = response.getFormWithID("LoginForm");
    request = loginForm.getRequest();
    request.setParameter("username", "test");
    request.setParameter("password", "test");
    response = conversation.getResponse(request);
    text = response.getText();
    assertTrue("Checking succesful login", text.contains(success));
  }

  /**
   * Test the security filter.
   * 
   * @throws Exception If problem occurs.
   */
  @Test
  public void testSecurityFilter() throws Exception {
    WebConversation conversation = new WebConversation();

    // Get login page and check title
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Checking reroute to index.jsp retrieval", "MyIsern 2.0", response.getTitle());
    String text = response.getText();
    assertTrue("Check for login.", text.contains("Username:"));
  }

}